﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.UserControl;
using Hisun.Controllers;
using System.Text;
using System.Collections.Generic;


namespace Hisun.View.PIPDeliverInfo
{
    public partial class PIPDiliverInfoCheckReport : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        List<int> MyCenterIDs = new List<int>();
        public string alter ;





        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist(DropDownList_Year, DropDownList_Month, Application["accountPeriod"].ToString());
                Panel_Main.Visible = false;

                string MyCurrentAccountPeriod = Application["accountPeriod"].ToString();

                db.CommandTimeout = 240;  //设定计算时间在4分钟以内完成。

                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();

     
                MyCenterIDs.Clear();
                var myquery = from a in db.TABC_JS_CostCenter select a;
                FillList(Convert.ToInt32(CurrentUserCenterId.责任部门ID), myquery);

                Session["list"] = MyCenterIDs;


            

            }
            else
            {
                MyCenterIDs = (List<int>)Session["list"];

            }
        }

        //查询当前操作员对应的产品ID
        private void FillList(int CenterID, IEnumerable<TABC_JS_CostCenter> TotalTrees)
        {
            var Mylist = (from m in TotalTrees where m.上级作业中心ID == CenterID select m).ToList();

            if (Mylist.Count() == 0)
                return;
            foreach (var MylistItem in Mylist)
            {
                MyCenterIDs.Add(MylistItem.作业中心ID);
                FillList(MylistItem.作业中心ID, TotalTrees);

            }
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string MyQueryAccountPeriod = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); 
  

            var IsValidString = from a in db.TABC_JS_AccountPeriods
                                where a.会计期间ID == MyQueryAccountPeriod
                                select a;
            if (IsValidString.Count() == 0)
            {
                
                alter = "<script>alert('会计期间不存在，请检查输入是否正确');</script>";
                Panel_Main.Visible = false;
                return;
            }
            else if (IsValidString.Count() == 1)
            {
                var MyQuery = from a in db.TABC_JS_ProductInProcessDelivery
                              join b in db.TABC_JS_CostCenter on a.作业中心ID equals b.作业中心ID
                              join c in db.TABC_JS_ProductInProcess on a.中间体ID equals c.中间体ID
                              where a.会计期间ID == MyQueryAccountPeriod  && MyCenterIDs.Contains(a.作业中心ID)
                             
                              orderby a.作业中心ID, a.中间体ID
                              select new
                              {
                                 // a.作业中心ID,
                                  b.作业中心名称,
                                  a.中间体ID,
                                  中间体名称 = (c.中间体名称.Length > 20 ? c.中间体名称.Substring(0, 20) + "..." : c.中间体名称),
                                  a.转出数量,
                                  c.是否中间体,
                                  c.产品ID,
                                  转出批数 = string.Format("{0:N2}",a.转出批数),
                                  结存批数= string.Format("{0:N2}",a.结存批数),
                                  损失批数= string.Format("{0:N2}",a.损失批数),
                                  转出批时= string.Format("{0:N2}",a.转出批时),
                                  结存批时 = string.Format("{0:N2}", a.结存批时),
                                  损失批时 = string.Format("{0:N2}", a.损失批时),
                                  //这里期初批时，需要判断是否为空。
                                  期初批时 = string.Format("{0:N2}", (a.期初批时 == null ? 0:a.期初批时)),
                                  批时检查 = ((a.结存批时 + a.损失批时 + a.转出批时) >= (a.期初批时== null ? 0 : a.期初批时) ? "通过" : "<font color=red>未通过</font>")

                              };

                var MyQuery1 = from c1 in MyQuery select c1;
                if (CheckBox_WrongOnly.Checked == true)
                {

                    MyQuery1 = from d in MyQuery
                               where d.批时检查 != "通过"
                               select d;

                }


                var  MyQuery2 = from c2 in MyQuery1 select c2;

                if (CheckBox_FinalPIP.Checked == true)
                {

                    MyQuery2 = from d in MyQuery2
                               where d.是否中间体 == false
                               select d;
                }


                var MyQuery3 = from c3 in MyQuery2 select c3;

                if (CheckBox_Zero.Checked == true)
                {
                    MyQuery3 = from h in MyQuery3
                               where h.转出数量 > 0
                               select h;
                }

                var MyQuery4 = from c4 in MyQuery3 select c4;
                string MyProductId = TextBox_ProductId.Text;
                if (MyProductId != "")
                {
                    MyQuery4 = from i in MyQuery4
                               where i.产品ID.StartsWith(MyProductId)
                               select i;
                
                }

                var MyQuery5 = from c5 in MyQuery4 select c5;
                string MyCenterName = TextBox_CenterName.Text;
                if (MyCenterName != "")
                {
                    MyQuery5 = from j in MyQuery5
                               where j.作业中心名称.Contains(MyCenterName)
                               select j;
                }





                if (MyQuery5.Count() == 0)
                {

                    alter = "<script>alert('没有未通过检查的W系数信息');</script>";
                    Panel_Main.Visible = false;
                    return;

                }
                else
                {


                    Panel_Main.Visible = true;
                    GridView_PIPDiliverCheckReport.DataSource = MyQuery5;
                    GridView_PIPDiliverCheckReport.DataBind();



                }
             
            }
        }
    }
}
